home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2001 December / pcwk12201b.iso / Wersje pelne i specjalne / Winamp 2.77 i 3.0beta / wasabi-sdk_beta1.exe / studio / common / depend.h < prev    next >
C/C++ Source or Header  |  2001-10-08  |  6KB  |  177 lines

  1. /*
  2.  
  3.   Nullsoft WASABI Source File License
  4.  
  5.   Copyright 1999-2001 Nullsoft, Inc.
  6.  
  7.     This software is provided 'as-is', without any express or implied
  8.     warranty.  In no event will the authors be held liable for any damages
  9.     arising from the use of this software.
  10.  
  11.     Permission is granted to anyone to use this software for any purpose,
  12.     including commercial applications, and to alter it and redistribute it
  13.     freely, subject to the following restrictions:
  14.  
  15.     1. The origin of this software must not be misrepresented; you must not
  16.        claim that you wrote the original software. If you use this software
  17.        in a product, an acknowledgment in the product documentation would be
  18.        appreciated but is not required.
  19.     2. Altered source versions must be plainly marked as such, and must not be
  20.        misrepresented as being the original software.
  21.     3. This notice may not be removed or altered from any source distribution.
  22.  
  23.  
  24.   Brennan Underwood
  25.   brennan@nullsoft.com
  26.  
  27. */
  28.  
  29. #ifndef _DEPEND_H
  30. #define _DEPEND_H
  31.  
  32. #include "common.h"
  33. #include "ptrlist.h"
  34.  
  35. // a pair of classes to implement data dependency. a viewer can register
  36. // a list of things it wants to know about
  37.  
  38. class Dependent;
  39.  
  40. namespace DependentCB {
  41.   enum {
  42.     NOP=0,
  43.     DELETED=10,
  44.     ATTRADDED=20,
  45.     ATTRCHANGED=30,
  46.     ATTRDELETED=40,
  47.     DATACHANGED=50,
  48.   };
  49. };
  50.  
  51. // hints for DEPENDENT_CB_DATACHANGED
  52. #define DEPENDENT_HINT_REPAINT    1
  53. #define DEPENDENT_HINT_RELOAD    2
  54. #define DEPENDENT_HINT_OBJECTSPECIFIC    100//object-specific hints
  55.  
  56. // hint2 for DEPENDENT_HINT_RELOAD
  57. #define DEPENDENT_HINT2_RELOAD_FULL    0    // you should completely reload
  58. #define DEPENDENT_HINT2_RELOAD_ADD    1    // stuff was added
  59. #define DEPENDENT_HINT2_RELOAD_DEL    2    // stuff was deleted
  60.  
  61. // DO NOT DERIVE DIRECTLY FROM THIS!
  62. class NOVTABLE DependentViewer {
  63. public:
  64.   // item calls when it changes or disappears, or whatever
  65.   virtual int dependentViewer_callback(int cb, Dependent *item, int param1, int param2)=0;
  66. };
  67.  
  68. // inherit from this one
  69. class NOVTABLE COMEXP DependentViewerI : public DependentViewer {
  70. protected:
  71.   DependentViewerI();
  72.   DependentViewerI(const DependentViewerI &dep);
  73.   virtual ~DependentViewerI();
  74.  
  75. public:
  76.   // don't override this; override the individual convenience callbacks
  77.   virtual int dependentViewer_callback(int cb, Dependent *item, int param1, int param2);
  78.  
  79.   // copy
  80.   DependentViewerI& operator =(const DependentViewerI &dep);
  81.  
  82. protected:
  83.   // derived classes call this on themselves when they want to view a new item
  84.   // everything else gets handled automagically
  85.   void viewer_addViewItem(Dependent *item);
  86.   void viewer_delViewItem(Dependent *item);
  87.   void viewer_delAllItems();
  88.  
  89.   // call this whenever you need to know what you're looking at
  90.   Dependent *viewer_enumViewItem(int which);
  91.   int viewer_getNumItems();
  92.   // returns TRUE if item is in our list
  93.   int viewer_haveItem(Dependent *item);
  94.  
  95. // convenience callback methods
  96.  
  97.   // item you were looking at is gone: WARNING: pointer no longer valid!
  98.   virtual int viewer_onItemDeleted(Dependent *item) { return 1; }
  99.   // item you are looking at has a changed attribute
  100.   virtual int viewer_onItemAttrChange(Dependent *item, const char *attr) { return 1; }
  101.   // item you are looking at has changed data
  102.   virtual int viewer_onItemDataChange(Dependent *item, int hint1, int hint2) { return 1; }
  103.  
  104. private:
  105.   typedef PtrList<Dependent> DependentList;
  106.   DependentList *viewed_items;
  107. };
  108.  
  109. template <class VT>
  110. class DependentViewerT : private DependentViewerI {
  111. public:
  112.   void viewer_addViewItem(VT *item) { DependentViewerI::viewer_addViewItem(item); }
  113.   void viewer_delViewItem(VT *item) { DependentViewerI::viewer_delViewItem(item); }
  114.   using DependentViewerI::viewer_delAllItems;
  115.   VT *viewer_enumViewItem(int which) { return static_cast<VT*>(DependentViewerI::viewer_enumViewItem(which)); }
  116.   using DependentViewerI::viewer_getNumItems;
  117.   int viewer_haveItem(VT *item) { return DependentViewerI::viewer_haveItem(item); }
  118.  
  119.   virtual int viewer_onItemDeleted(VT *item) { return 1; }
  120.   virtual int viewer_onItemAttrChange(VT *item, const char *attr) { return 1; }
  121.   virtual int viewer_onItemDataChange(VT *item, int hint1, int hint2) { return 1; }
  122.  
  123. private:
  124.   virtual int viewer_onItemDeleted(Dependent *item) {
  125.     return viewer_onItemDeleted(static_cast<VT*>(item));
  126.   }
  127.   virtual int viewer_onItemAttrChange(Dependent *item, const char *attr) {
  128.     return viewer_onItemAttrChange(static_cast<VT*>(item), attr);
  129.   }
  130.   virtual int viewer_onItemDataChange(Dependent *item, int hint1, int hint2) {
  131.     return viewer_onItemDataChange(static_cast<VT*>(item), hint1, hint2);
  132.   }
  133. };
  134.  
  135. // ------------------------------------------------------------
  136.  
  137. // don't derive directly from this one
  138. class NOVTABLE Dependent {
  139. public:
  140.   // anyone can call this
  141.   virtual const char *dependent_getName()=0;
  142.  
  143.   // the viewer calls this
  144.   virtual void dependent_addViewer(DependentViewer *viewer)=0;
  145.   virtual void dependent_delViewer(DependentViewer *viewer)=0;
  146. };
  147.  
  148. class COMEXP NOVTABLE DependentI : public Dependent {
  149. protected:
  150.   DependentI();
  151.   DependentI(const DependentI &dep);
  152.   virtual ~DependentI();
  153.  
  154. public:
  155.   virtual const char *dependent_getName() { return NULL; }
  156.  
  157.   // copy
  158.   DependentI& operator =(const Dependent &dep);
  159.  
  160. protected:
  161.   virtual void dependent_addViewer(DependentViewer *viewer);
  162.   virtual void dependent_delViewer(DependentViewer *viewer);
  163.  
  164.   // call these on yourself when appropriate
  165.   void dependent_addedAttr(const char *name);
  166.   void dependent_changedAttr(const char *name);
  167.   void dependent_deletedAttr(const char *name);
  168.   void dependent_changedData(int hint1=0, int hint2=0);
  169.  
  170. private:
  171.   void sendViewerCallbacks(int msg, int param1=0, int param2=0);
  172.   typedef PtrList<DependentViewer> ViewerList;
  173.   ViewerList *viewers;
  174. };
  175.  
  176. #endif
  177.